﻿using EventSubscriptionExample;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Interview.Model
{
    public class MeSwitch
    {
        public List<IElectricalBase> electricals = new List<IElectricalBase>();

        public void AddAppliance(IElectricalBase electrical)
        {
            this.electricals.Add(electrical);

            // 订阅状态变化事件
            electrical.StatusChanged += Appliance_StatusChanged;
        }

        private void Appliance_StatusChanged(object sender, ElectricalStatusEventArgs e)
        {
            Console.WriteLine($"[状态变化] {e.Name}: {e.OldStatus} -> {e.NewStatus}");
        }

        public void TurnOn()
        {
            Console.WriteLine("开关打开，所有电器通电");
            foreach (var appliance in electricals)
            {
                appliance.TurnOn();
            }
        }

        public void TurnOff()
        {
            Console.WriteLine("开关关闭，所有电器断电");
            foreach (var appliance in electricals)
            {
                appliance.TurnOff();
            }
        }

        public void CheckAllElectrical()
        {
            Console.WriteLine("检查所有电器状态...");
            foreach (var appliance in electricals)
            {
                appliance.CheckStatus();
            }
        }
    }
}
